﻿namespace SGLib.Scene
{
    public class SceneManager : Singleton<SceneManager>
    {
        public BaseScene CurScene;
        public  BaseScene PreviousScene { get; private set; }
        public T Cur<T>() where T : BaseScene
        {
            return (T)CurScene;
        }
        public T LoadScene<T>(params object[] args) where T : BaseScene, new()
        {
            if (CurScene != null)
            {
                this.PreviousScene = CurScene;
                this.PreviousScene.OnExit();
                CurScene = null;
            }
            T newScene = new T();
            CurScene = newScene;
            newScene.OnInit(args);
            newScene.Enter();
            return newScene;
        }
        public T InitScene<T>(params object[] args) where T:BaseScene,new() 
        {
            T newScene = new T();
            newScene.OnInit(args);
            return newScene;
        }

        public void ChangeScene(BaseScene scene) 
        {
            if (CurScene != null) 
            {
                this.PreviousScene = CurScene;
                this.PreviousScene.OnExit();
            }
            this.CurScene = scene;
        }

        public void DestoryScene()
        {
            this.PreviousScene = this.CurScene;
            this.PreviousScene.OnExit();
            this.CurScene = null;
        } 
    }
}